package com.zhan.ielts.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zhan.ielts.dao.SubTableDao;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.zhan.frame.core.dao.impl.BaseDaoImpl;
import com.zhan.frame.core.exception.DaoException;
import com.zhan.ielts.dao.WordReviewDao;
import com.zhan.ielts.domain.WordReview;
import com.zhan.ielts.domain.vo.WordReviewVo;

/**
 * Created by Admin on 2016/1/8.
 */
@Repository("tWordReview")
public class WordReviewDaoImpl extends BaseDaoImpl<WordReview> implements WordReviewDao,SubTableDao {

	@Override
	public List<WordReview> selectWordReviewByCustom(WordReview entity) {
		return sqlSessionTemplate.selectList(getSqlName("selectByCustom"), entity);
	}

	@Override
	public void updateByCustom(List<WordReview> entityList) {
		try {
			if (entityList != null && !entityList.isEmpty()) {
				for (WordReview entity : entityList) {
					if (StringUtils.isBlank(entity.getId()))
						entity.setId(generateId());
					sqlSessionTemplate.insert(getSqlName("saveOrUpdate"), entity);
				}
			}
		} catch (Exception e) {
			throw new DaoException(String.format("添加对象出错！语句：%s", getSqlName("saveOrUpdate")), e);
		}
	}

	@Override
	public void saveOrUpdate(List<WordReviewVo> entityList) {
		try {
			if (entityList != null && !entityList.isEmpty()) {
				for (WordReview entity : entityList) {
					if (StringUtils.isBlank(entity.getId()))
						entity.setId(generateId());
					sqlSessionTemplate.insert(getSqlName("saveOrUpdate"), entity);
				}
			}
		} catch (Exception e) {
			throw new DaoException(String.format("添加对象出错！语句：%s", getSqlName("saveOrUpdate")), e);
		}
	}

	@Override
	public List<WordReview> selectLatest(WordReviewVo entity) {
		return sqlSessionTemplate.selectList(getSqlName("selectLatestOnes"), entity);
	}

	@Override
	public Long getCountByCutom(WordReviewVo entity) {
		return sqlSessionTemplate.selectOne(getSqlName("selectLatestCount"), entity);
	}

	@Override
	public void createSubTable(String tableName) {
		Map<String,Object> map = new HashMap<>(1);
		map.put("tableName",tableName);
		sqlSessionTemplate.update(getSqlName("createSubTable"),map);
	}

}
